package de.itagile.hypersphere
class Template {
	
	public static String path = ''
	
	public packageToClass(def text) {
		text.replace('.','/') << '.java'
	}
	
	public String extractPackage(def fqn) {
		return fqn.substring(0, fqn.lastIndexOf('.'))
	}
	
	public extractClass(def fqn) {
		return fqn - extractPackage(fqn) - '.'
	}

	protected printLine(def line) {
		println line
	}

	protected save(def file, def line) {
		file.append(line << '\n')
	}

	public file (filename, text) {
		filename = packageToClass(filename)
		File file = new File(path + filename)
		file.getParentFile().mkdirs()
		file.write('')
	    text.flatten().each {
	    	printLine(it)
	        save(file, it)
	    }
	}

	public toFirstUpper(def string) {
		new String(string[0].toUpperCase().concat(string[1..string.length()-1]))
	}
	
	public toFirstLower(def string) {
		new String(string[0].toLowerCase().concat(string[1..string.length()-1]))
	}

	public forall(def field, def template) {
		List list = new ArrayList()
		field.each{list.add(template(it))}
	    return list
	}
}